from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager


class New_Likes(models.Model):
    object_id = models.IntegerField()
    content_type = models.ForeignKey(ContentType)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)  # 点赞用户
    pub_date = models.DateTimeField(auto_now_add=True)  # 点赞时间

class  Lab(models.Model):
    resume_id = models.IntegerField(null=False)
    content = models.CharField(max_length=8)

class Prize(models.Model):
    resume_id = models.IntegerField(null=True,blank=True)
    content = models.CharField(max_length=200)


class Work(models.Model):
    resume_id = models.IntegerField(null=True,blank=True)
    name = models.CharField(max_length=50)
    position = models.CharField(max_length=50)
    content = models.CharField(max_length=200)


class Category(models.Model):
    content = models.CharField(max_length=50,unique=True)

    class Meta:
        verbose_name = '人才类型'
        verbose_name_plural = '人才类型'


    def __str__(self):
        return self.content


class Evaluation(models.Model):
    resume_id = models.IntegerField(null=True, blank=True)
    author = models.ForeignKey(settings.AUTH_USER_MODEL)
    content = models.CharField(max_length=50)


class Resume(models.Model):
    title = models.CharField(max_length=8, blank=True, verbose_name='姓名', default='')
    body = models.TextField(verbose_name='内容')
    author = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='resume_posts', on_delete=models.CASCADE)
    total_like = models.PositiveIntegerField(default=0, db_index=True)
    view_count = models.PositiveIntegerField(default=0, verbose_name='查看次数')
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    gender = models.CharField(max_length=30, choices=(("male", "男"), ("female", "女")), default="female",
                              verbose_name="性别")
    skill = TaggableManager(verbose_name='技能', help_text='技能标签 以,分割')
    category = models.ForeignKey(Category, verbose_name="类型", related_name='resume_category',default=1,
                                 on_delete=models.CASCADE)

    class Meta:
        verbose_name = '简历'
        verbose_name_plural = '简历'
        ordering = ('-created',)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('recommendation:resume', kwargs={'pk': self.pk})

    def get_comment_url(self):
        return reverse('two_comment:submit_comment', kwargs={'pk': self.pk})

    def get_reply_url(self):
        return reverse('two_comment:submit_reply', kwargs={'pk': self.pk})

    def get_summary(self):
        return self.body[:60]

    def view_count_increase(self):
        self.view_count += 1
        self.save(update_fields=['view_count'])
